import axios from "axios";

const state = {
    shopTypeList: [],     //店铺类别列表
    allshopTypeList: [],      //店铺类别列表所有数据
    shopList: [],             //店铺列表
    shopListByTypeId: []       //存放的是根据店铺类别查找到的店铺。
}

const mutations = {
    //将数据添加到shopTypeList中
    CHANGE_SHOP_TYPE_LIST(state, shopTypeList = 1) {
        state.shopTypeList = shopTypeList;
    },
    //将获取到的店铺类型的所有数据加到allshopTypeList中
    CHANGE_ALL_TYPE_LIST(state, allshopTypeList) {
        state.allshopTypeList = allshopTypeList;
    },
    //将数据添加到shopList中
    CHANGE_SHOP_LIST(state, shopList) {
        state.shopList = shopList;
    },
    //将根据店铺类别查找到的店铺存入shopListByTypeId
    CHANGE_SHOPLIST_TYPEID(state, shopListByTypeId) {
        state.shopListByTypeId = shopListByTypeId
    }
}

const actions = {
    //获取店铺类型列表数据
    getshopTypeList({ commit }, params) {
        axios.get('/shopTypeList', { params })
            .then((res) => {
                // console.log(res.pageIndex);
                if (res.ok === 1) {
                    commit('CHANGE_SHOP_TYPE_LIST', res.shopTypeList);
                    commit('CHANGE_PAGE_INDEX', res.pageIndex);
                }
            });
    },
    //获取店铺类型列表所有数据
    allgetShopTypeList({ commit }) {
        axios.get('/allgetTypeList').then((res) => {
            // console.log(res);
            commit('CHANGE_ALL_TYPE_LIST', res.shopTypeList);
        })
    },
    //获取店铺列表数据
    getshopList({ commit }, params) {
        axios.get('/shopList', { params }).then((res) => {
            if (res.ok === 1) {
                // console.log(res);
                commit('CHANGE_SHOP_LIST', res.getList);
                commit('CHANGE_PAGE_INDEX', res.pageIndex);
            }
        })
    },
    //根据店铺类别查找到的店铺
    allgetshopListByTypeId({ commit }, shopTypeId) {
        // console.log(shopTypeId);
        axios.get('/shopList/' + shopTypeId).then((res) => {
            // console.log(res);
            commit('CHANGE_SHOPLIST_TYPEID', res.shopList);
        })
    },
    //删除店铺类型记录
    deleteshopTypeList(state, _id) {
        // console.log(state);
        return axios.delete('/shopTypeList', {
            params: {
                _id
            }
        });
    },
    //删除店铺记录
    deleteshopList(state, _id) {
        return axios.delete('/shopList', {
            params: {
                _id
            }
        });
    }
}


export default {
    state, mutations, actions
}